* This do-file creates a table of results from the output support experiment
* Paper Table 6

tempfile treats

*****
* Household treatment assignemnts
*****

use "$out_data/fpc_procurement.dta", clear
merge m:1 block_name village_name using "$admin_data/treatment_assignment.dta", keepusing(block village treat_*)
	replace treat_price=0 if FarmerID==2190075 // block_name=="Rivilganj" & village_name=="Mahamadpur"
	drop if _m==2
	drop _m
	
gen byte sub = treat_price==1 if !missing(treat_price)
gen byte msp = treat_price==2 if !missing(treat_price)
gen byte fpc_crop = (cropcode==4 | cropcode==3) 
gen byte sub_c = sub*fpc_crop
gen byte msp_c = msp*fpc_crop

label var sub "Subsidy"
label var sub_c "Subsidy"
label var msp "Floor"
label var msp_c "Floor"

	
order fpc_input_area fpc_input_seed fpc_output_prod fpc_output_sold, last
	
*****
* Table for Paper
*****
* Within-housheold inputs, total village outputs

eststo clear
cap mat drop P Q
forvalues s = 1/2 {
	foreach vbl of varlist fpc_input* {
		eststo `vbl'`s': areg `vbl' sub_c msp_c i.cropcode if season==`s', a(FarmerID) vce(cluster village)
		mat tmp = r(table)
		quietly sum `vbl' if treat_price==0 & season==`s' & fpc_crop==1
		estadd scalar ctrl_mean = `r(mean)'
		estadd local FE_HH X
		
		* Save p-values for qqvalue
		mat P = nullmat(P) \ tmp[4,1..2]'
	}
	
	preserve
		local vbl fpc_output_sold
		keep if cropcode==3 | (cropcode==4)
		collapse sub msp treat_* (sum) `vbl', by(block village season) fast
		rename (sub msp) (sub_c msp_c)
		eststo `vbl'`s': areg `vbl' sub msp if season==`s', a(block) vce(cluster village)
		mat tmp = r(table)
		quietly sum `vbl' if treat_price==0 & season==`s'
		estadd scalar ctrl_mean = `r(mean)'
		
		* Save p-values for qqvalue
		mat P = nullmat(P) \ tmp[4,1..2]'
	restore
}

preserve 
clear
svmat P
qqvalue P1, qvalue(qval) method(yekutieli)
mkmat qval, mat(Q)
restore

* Save q-values to estimation output
local i = 1
forvalues s = 1/2 {
foreach vbl of varlist fpc_input* fpc_output_sold {
	
	local j = `i'+1
	mat q_val = Q[`i'..`j',1]'
	mat colnames q_val = sub_c msp_c
	estadd matrix q_val : `vbl'`s'
	local i = `i'+2
}
}
//esttab using "$out_tables/reg_fpc_table.tex", replace se tex ///
//		stats(ctrl_mean FE_HH r2 N) keep(sub_c msp_c) label

label var sub_c "Subsidy"
label var msp_c "Price Floor"

local estset replace se tex label  mlabels(none) nonotes nonumbers starlevels(* 0.1 ** 0.05 *** 0.01)
local mainstats stats(ctrl_mean FE_HH r2 N, fmt(2 0 2 0) labels("Control Mean" "HH FEs" "R-Squared" "Observations")) 
local ecell ""
//local ecell gaps collab(none) cells(b(fmt(4 3 2 4 3 2) pvalue(q_val) star) se(fmt(2) par) q_val(fmt(3) par({[ ]})))

local mgset prefix(\multicolumn{@span}{c}{) suffix(}) span
local col1 "  & Area & Sown & Sold & Area & Sown & Sold \\"
local col2 " & (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule "
local rules \cmidrule(lr){@span}
local est_adopt mgroups("Kharif Season" "Rabi Season", pattern(1 0 0 1 0 0) `mgset' erepeat( `rules') ) posthead(`col1' `col2')

esttab using "$out_tables/Table_6.tex", keep(sub_c msp_c) `estset' `mainstats' `ecell' `est_adopt'


